package com.cloudkinto.controller.wr;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.wr.WhRentPageVo;
import com.cloudkinto.vo.ft.FreightStatusUpdateReq;
import com.cloudkinto.vo.wr.WhRentAddReq;
import com.cloudkinto.vo.wr.WhRentInfoRes;
import com.cloudkinto.vo.wr.WhRentUpdateReq;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.wr.WarehouseRentService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2024-11-14
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "仓储费omp模块")
@RequestMapping("/api/admin/omp/WarehouseRent")
public class WarehouseRentController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(WarehouseRentController.class);

    private final WarehouseRentService service;

    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated WhRentAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功",service.add(req,userId, getTenantId()));
    }

    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated WhRentUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功",service.update(req,userId, getTenantId()));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id,userId, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WhRentInfoRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "rentType", value = "仓储费类型"),
            @ApiImplicitParam(name = "currencyCode", value = "币种 数组", type = "arr"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "ruleName", value = "仓储费名称"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WhRentPageVo.class))
    public SingleResult list(HttpServletRequest request){
        Map<String, Object> map = this.getMap(request);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "rentType", value = "仓储费类型"),
            @ApiImplicitParam(name = "currencyCode", value = "币种 数组", type = "arr"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "ruleName", value = "仓储费名称"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = WhRentPageVo.class))
    public PageResult listPage(HttpServletRequest request){

        Map<String, Object> map = this.getMap(request);
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }


    public Map<String, Object> getMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String rentType = request.getParameter("rentType");
        String[] currencyCode = request.getParameterValues("currencyCode");
        String beginTime = request.getParameter("beginTime");
        String status = request.getParameter("status");
        String endTime = request.getParameter("endTime");
        String ruleName = request.getParameter("ruleName");
        map.put("rentType", rentType);
        if (currencyCode != null) {
            map.put("currencyCode", Arrays.asList(currencyCode));
        }
        map.put("status", status);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("ruleName", ruleName);
        map.put("tenantId", getTenantId());

        return map;
    }


    @UserSessionRequired
    @PutMapping("/updateStatus")
    @ApiOperation(value = "仓租费停用启用", notes = "仓租费停用启用")
    public SingleResult updateStatus(@RequestBody @Validated FreightStatusUpdateReq req) {
        Long userId = getCurrentUserId();
        service.updateStatus(req, userId, getTenantId());
        return SingleResult.success();
    }

}

